﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class ProjectExplorer : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public delegate void OpenFileHandler(object sender, OpenFileArgs e);
        public event OpenFileHandler OpenFile;

        public ProjectExplorer()
        {
            InitializeComponent();
            listView1.DoubleClick += new EventHandler(listView1_DoubleClick);
            visibleItems = new List<ListViewItem>();
        }

        public void AddFile(ProjectFile p)
        {
            var item = new ListViewItem(new string[] { p.Filename }, 1);
            item.Tag = p;
            listView1.Items.Add(item);
        }

        void listView1_DoubleClick(object sender, EventArgs e)
        {
            if(OpenFile != null)
                OpenFile(this, new OpenFileArgs((ProjectFile)listView1.SelectedItems[0].Tag));
        }

        private void ProjectExplorer_Resize(object sender, EventArgs e)
        {
            listView1.Columns[0].Width = listView1.ClientSize.Width;
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void ProjectExplorer_Load(object sender, EventArgs e)
        {
            //treeView1.ExpandAll();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem item in visibleItems)
                item.BackColor = Color.White;
            visibleItems.Clear();

            if (String.IsNullOrEmpty(textBox1.Text)) return;
            
            ListViewItem i = listView1.FindItemWithText(textBox1.Text);
            while(i != null && i.Index < listView1.Items.Count)
            {
                i.BackColor = Color.FromArgb(0, 153, 180, 209);
                visibleItems.Add(i);
                if (i.Index + 1 == listView1.Items.Count) break;
                i = listView1.FindItemWithText(textBox1.Text, true, i.Index + 1);
            }
        }

        List<ListViewItem> visibleItems;
    }

    public class OpenFileArgs : EventArgs
    {
        public ProjectFile file;

        public OpenFileArgs(ProjectFile f) { file = f; }
    }
}
